package org.theSeed.redisIntegration.config;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;
import org.redisson.api.NameMapper;
import org.theSeed.base.exp.ServiceExp;
import org.theSeed.base.pojo.constant.SysConstant;

public class RedissonKey implements NameMapper {
  /**
   * redisskey前缀
   */
  @Getter
  private static String REDIS_KEY_PREIX = SysConstant.EMPTY_STR;

  public static void setRedisKeyPreix(String redisKeyPreix) {
    if (ObjectUtil.isEmpty(REDIS_KEY_PREIX)){
      REDIS_KEY_PREIX = redisKeyPreix;
      return;
    }
    throw new ServiceExp("系统缓存key已配置");
  }

  @Override
  public String map(String name) {
    return REDIS_KEY_PREIX + name;
  }

  @Override
  public String unmap(String name) {
    if (name.indexOf(REDIS_KEY_PREIX) == 0){
      return name.substring(REDIS_KEY_PREIX.length(),name.length()-1);
    }
    throw new ServiceExp("非系统缓存");
  }
}